<?php
namespace OrderAdmin\Controller;
use OrderAdmin\Model\ActivityModel;
use OrderAdmin\Model\AuditModel;
use OrderAdmin\Model\OrderModel;
use OrderAdmin\Model\PartnerModel;
use OrderAdmin\Model\StoreModel;
use OrderAdmin\Model\WorkerModel;
use Think\PageAdmin;

/**
 * 师傅管理控制器
 */
class AuditController extends BaseController
{

    /**
     * 初始化
     *
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->auditModel = new AuditModel();
        $this->storeModel = new StoreModel();
        $this->activityModel = new ActivityModel();
    }

    /**
     * 审核员
     * @return [type] [description]
     */
    public function index()
    {
        $count = $this->auditModel->getAllCount();      //总数
        $thisMonthCount = $this->auditModel->getCountThisMonth();
        $this->seotitle = '客户列表';
        $this->assign(compact( 'count', 'thisMonthCount'));
        $this->display();
    }

    /**
     * 项目管理
     */
    public function project(){
        $this->display();
    }

    /**
     * 项目管理详情页
     */
    public function shopinfo(){
        $id = I('id', '');
        $this->assign(compact('id'));
        $this->display();
    }

    /**
     * 分页数据
     */
    public function getData(){
        $param['key'] = I('post.key', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $data = $this->auditModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

    /**
     * 师傅详情
     */
    public function detail(){
        $id = I('get.id', 0, 'intval');
        if($id === 0){
            $this->error('客户不存在');
        }
        $data = $this->auditModel->getInfoById($id);
        if($data){
            $this->assign(compact('data', 'id'));
            $this->display();
        }else{
            $this->error('获取信息失败');
        }
    }

    /**
     * 获取门店
     */
    public function getStore(){
        $param['audit_user_id'] = I('post.audit_user_id', '');
        $param['key'] = I('post.key', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $data = $this->storeModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

    /**
     * 获取当前审核员的活动
     */
    public function getActivityData(){
        $param['audit_user_id'] = I('post.audit_user_id', '');
        $param['key'] = I('post.key', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $data = $this->activityModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

    public function add(){
        $this->display();
    }

    public function edit(){
        $id = I('get.id', 0, 'intval');
        if($id === 0){
            $this->error('审核员不存在');
        }
        $this->assign(compact('id'));
        $this->display();
    }

    /**
     * 修改与添加
     */
    public function update(){
        $type = I('post.type', '');
        $param['company_ak'] = I('post.company_ak', '');
        $param['name'] = I('post.name', '');
        $param['mobile'] = I('post.mobile', '');
        $param['password'] = I('post.password', '');
        $param['company_name'] = I('post.company_name', '');
        $param['corporation_name'] = I('post.corporation_name', '');
        $param['telephone'] = I('post.telephone', '');
        $param['province'] = I('post.province', '');
        $param['city'] = I('post.city', '');
        $param['area'] = I('post.area', '');
        $param['address'] = I('post.address', '');
        $param['industry'] = I('post.industry', '');
        $param['business_license'] = I('post.business_license', '');
        $param['contract'] = I('post.contract', '');
        $param['certid_front'] = I('post.certid_front', '');
        $param['certid_side'] = I('post.certid_side', '');
        $param['project_manager_id'] = I('post.project_manager_id', '');
        $metalList = I('post.metalList', []);
        if($type == 'add'){
            $param['password'] = hash_make($param['password']);
            $param['add_time'] = time();
            $param['update_time'] = time();
            // 添加基本信息
            $res1 = $this->auditModel->add($param);
            // 添加材料
            $res2 = true;
            if($metalList){
                $res2 = $this->auditModel->editMateriel($res1, $metalList);
            }
            if($res1 && $res2!==false){
                ajax_return(true, '添加成功');
            }else{
                ajax_return(false, '添加失败');
            }
        }elseif($type == 'edit'){
            $id = I('post.id', 0, 'intval');
            if($id === 0){
                ajax_return(false, '审核员不存在');
            }
            if($param['password']){
                $param['password'] = hash_make($param['password']);
            }
            // 修改基本信息
            $res1 = $this->auditModel->where(['id'=>$id])->save($param);
            // 修改材料
            if($metalList){
                $res2 = $this->auditModel->editMateriel($id, $metalList);
            }
            if($res1!==false && $res2!==false){
                ajax_return(true, '修改成功');
            }else{
                ajax_return(false, '修改失败');
            }
        }
    }

    /**
     * 获取详情
     */
    public function getInfoById(){
        $id = I('post.id', 0, 'intval');
        if($id === 0){
            $this->error('审核员不存在');
        }
        $data = $this->auditModel->alias('a')
            ->field('a.*, 
            b.filepath business_license_bak, c.filepath contract_bak, d.filepath certid_front_bak, e.filepath certid_side_bak, f.filepath headimg_bak,
            g.nickname')
            ->join('LEFT JOIN __FILES__ b ON a.business_license = b.file_id')
            ->join('LEFT JOIN __FILES__ c ON a.contract = c.file_id')
            ->join('LEFT JOIN __FILES__ d ON a.certid_front = d.file_id')
            ->join('LEFT JOIN __FILES__ e ON a.certid_side = e.file_id')
            ->join('LEFT JOIN __FILES__ f ON a.headimg = f.file_id')
            ->join('LEFT JOIN __ADMINS__ g ON a.project_manager_id = g.id')
            ->where(['a.id'=>$id])->find();
        $data['full_business_license'] = makeImagePath($data['business_license_bak']);
        $data['full_contract'] = makeImagePath($data['contract_bak']);
        $data['full_certid_front'] = makeImagePath($data['certid_front_bak']);
        $data['full_certid_side'] = makeImagePath($data['certid_side_bak']);
        $data['full_headimg'] = makeImagePath($data['headimg_bak']);
        ajax_return(true, '', $data);
    }

    /**
     * 添加修改材料
     */
    public function editMateriel(){
        $auditId = I('post.auditId', 0, 'intval');
        if($auditId === 0){
            ajax_return(false, '审核员不存在');
        }
        $param = I('post.jsonStr', '', '');
        if(!$param){
            ajax_return(false, "请添加材料");
        }
        $res = $this->auditModel->editMateriel($auditId, $param);
        $res ? ajax_return(true, '操作成功') : ajax_return(false, '操作失败');
    }

    public function getMateriels(){
        $id = I('post.id', 0, 'intval');
        if($id === 0){
            ajax_return(false, '审核员不存在');
        }
        $data = $this->auditModel->getMateriels($id);
        ajax_return(true, '', $data);
    }
}